package TUILayer;
import java.util.Scanner;
import ControlLayer.*;
import ModelLayer.*;
/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    DVDCtr dvdCtr;
    //DVD UI must know the DVD controller 

   
    public DvdUI()
    {
        dvdCtr = new DVDCtr();
        
    }
    
    public void start()
    {
        dvdMenu();
    }
    
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice = writeDVDMenu();
            if (choice == 1)
            {
                   int id = inputDVDNo();
                String title = inputTitle();
                String publicationDate = inputDVDPublicationDate();
                String author = inputAuthor();
                createDVD(id, title, publicationDate, author);
                updateDVD(id, title,  author, publicationDate);
            }
            
            else
            {
                if(choice == 2)
                {
                int id = inputDVDNo();
                dvdCtr.findDVD(id);
                if(findDVD(id) != null)
                {
                    DVD dvd = dvdCtr.findDVD(id);
                    String title = dvd.getTitle();
                    String author = dvd.getAuthor();
                    showTitle(title);
                    showAuthor(author);
                
                }
                    else
                    {
                     showErrorInformation();
                }
                waitForInput();
            }
                else{
                    if(choice == 3)
                        {
                int id = inputDVDNo();
                dvdCtr.findDVD(id);
                if(findDVD(id) != null)
                {
                    DVD dvd = dvdCtr.findDVD(id);
                    String title = inputTitle();
                    String author = inputAuthor();
                    String publicationDate = inputDVDPublicationDate();
                    updateDVD(id, title,  author, publicationDate);
                }
                    else
                    {
                        showErrorInformation();
                    }
                    waitForInput();
                    }
                    else{
                        if(choice == 4){
                            int id = inputDVDNo();
                dvdCtr.findDVD(id);
                if(findDVD(id) != null)
                            dvdCtr.deleteDVD(id);
                             else{
                            showErrorInformation();
                        }
                        waitForInput();
                    }
                   else{   
                            if(choice == 5)
                            {
                                if( dvdCtr.showDVDListSize() != 0)
                                {
                                printAllDVDs();
                            }
                            else
                            {
                                System.out.println("There are no dvds");
                                waitForInput();
                            }
                            waitForInput();
                        }
                        else
                        {
                            if(choice == 6)
                            { 
                            
                                System.out.println("Now you are creating the copy of dvd ");
                                int id = inputDVDNo();
                                dvdCtr.findDVD(id);
                                if(findDVD(id) == null)
                                {
                                    showErrorInformation();
                                }
                                else
                                {
                                int serialNumberOfCopy = inputCopyNo();
                                String purchaseDateOfCopy = inputCopyPurchaseDate();
                                double purchasePriceOfCopy = inputCopyPrice();
                                createCopy(id, serialNumberOfCopy, purchaseDateOfCopy, purchasePriceOfCopy);
                            }
                             waitForInput();
                            }
                            else
                            {
                                if(choice == 7)
                                {
                                    int id = inputDVDNo();
                                dvdCtr.findDVD(id);
                                if(findDVD(id) != null)
                                {
                                    int serialNumberOfCopy = inputCopyNo();
                                    if(findCopy(id, serialNumberOfCopy) != null)
                                    {
                                        Copy copy = findCopy(id, serialNumberOfCopy);
                                        System.out.println("The copy serial number is " + copy.getSerialNumber());
                                        System.out.println("The copy purchase date is " + copy.getPurchaseDate());
                                        System.out.println("The copy purchase price is " + copy.getPurchasePrice());
                                        System.out.println("The information about dvd:");
                                                            DVD dvd = dvdCtr.findDVD(id);
                    String title = dvd.getTitle();
                    String author = dvd.getAuthor();
                    showTitle(title);
                    showAuthor(author);
                                    }
                                    else
                                    {
                                        System.out.println("There is no copy with this serial number");
                                    }
                                }
                                    else 
                                    {
                                        showErrorInformation();
                                    }
                                
                                waitForInput();
                            }
                            else
                            {
                                if(choice == 8)
                                {
                                int id = inputDVDNo();
                                dvdCtr.findDVD(id);
                                if(dvdCtr.findDVD(id) == null)
                                {
                                    System.out.println("There is no dvd with that id");
                                }
                                else
                                {
                                int serialNumberOfCopy = inputCopyNo();
                                findCopy(id,  serialNumberOfCopy);
                                if(findCopy(id,  serialNumberOfCopy) == null)
                                {
                                    System.out.println("There is no copy with this serial number");
                                }
                                else
                                {
                                
                                String purchaseDateOfCopy = inputCopyPurchaseDate();
                                double purchasePriceOfCopy = inputCopyPrice();
                                dvdCtr.updateCopy(id, serialNumberOfCopy, purchaseDateOfCopy, purchasePriceOfCopy);
                            }}
                            waitForInput();
                        }
                        else
                        {
                            if(choice == 9)
                            {
                            int id = inputDVDNo();
                                dvdCtr.findDVD(id);
                                  if(dvdCtr.findDVD(id) == null)
                                {
                                    System.out.println("There is no dvd with that id");
                                }
                                else
                                {
                                int serialNumberOfCopy = inputCopyNo();
                                findCopy(id,  serialNumberOfCopy);
                                if(findCopy(id,  serialNumberOfCopy) == null)
                                {
                                    System.out.println("There is no copy with this serial number");
                                }
                                else
                                {
                                    deleteCopy( id, serialNumberOfCopy);
                                }}
                             waitForInput();
                            }
                                if (choice == 10)
                                {
                                    exit = true;
                                }
                                else
                                {
                                    System.out.println("You can not do that, sorry Sir/Madame");
                                waitForInput();
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
        }}}}}}//end while

                
        
    
    private int writeDVDMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** DVD Menu ***");
            System.out.println(" (1) Create DVD");
            System.out.println(" (2) Show DVD description");
            System.out.println(" (3) Update DVD ");
            System.out.println(" (4) Delete DVD");
            System.out.println(" (5) Show current DVD list");
            System.out.println(" (6) Crate copy");
            System.out.println(" (7) Show copy description");
            System.out.println(" (8) Update copy");
            System.out.println(" (9) Delete copy");
            System.out.println(" (10) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }

         
        
    private double inputCopyPrice()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type copy price: ");
        double copyPrice = keyboard.nextInt();
        return copyPrice;
    }
    
        private String inputCopyPurchaseDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type copy purchase date: ");
        String purchaseDate = keyboard.nextLine();
        return purchaseDate;
    }

    private int inputDVDNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD number: ");
        int no = keyboard.nextInt();
        return no;
    }
        private String inputDVDPublicationDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD publication date: ");
        String publicationDate = keyboard.nextLine();
        return publicationDate;
    }
        
    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }
    private String inputAuthor()
    {
            Scanner keyboard = new Scanner(System.in);
        System.out.println("Type author's name: " );
        String artist = keyboard.nextLine();
        return artist;
    }
    
    private int inputCopyNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the copy serial number: ");
        int no = keyboard.nextInt();
        return no;
    }
    
    //there is need for more methods to read information about DVDs
    //and methods to display the information
  
    public DVD findDVD(int id)
    {     
        DVD dvd = dvdCtr.findDVD(id);
        if(dvd != null)
        {
            return dvd;
            }
        else
        {
            return null;
        }

    }
    public void createDVD(int id, String title, String artist, String publicationDate)
    {
     dvdCtr.createDVD(id,  title,  artist,  publicationDate);
    }
    public void createCopy(int idOfDvd,int serialNumber, String purchaseDate, double purchasePrice)
    {
        dvdCtr.createCopy(idOfDvd, serialNumber, purchaseDate, purchasePrice);

    }
    private void updateDVD(int id, String title, String author, String publicationDate)
    { 
        dvdCtr.updateDVD(id, title,  author,  publicationDate);
    }
    private void waitForInput()
    {
        System.out.println("Press Enter to continue");
        Scanner keyboard = new Scanner(System.in);
        keyboard.nextLine();
    }
    private void deleteDVD(int id)
    {
      dvdCtr.deleteDVD(id);
    }
        public void showTitle(String title)
    { 
        System.out.println("The title of the DVD: " + title);
    }
    public void showAuthor(String author)
    { 
        System.out.println("The autor of the DVD: " + author);
    }
    public Copy findCopy(int id, int serialNumber)
    {
        return dvdCtr.findCopy(id, serialNumber);
    }
    public void updateCopy(int id, int serialNumber, String purchaseDate, double purchasePrice)
   {
       dvdCtr.updateCopy(id, serialNumber, purchaseDate, purchasePrice);
    
}
public void  deleteCopy(int id, int serialNumber)
{
    dvdCtr.deleteCopy(id, serialNumber);
}
public void showErrorInformation()
{
    System.out.println("There is no dvd with this id");
}
public void printAllDVDs()
{
    dvdCtr.listAllDvds();
}
}
